package cn.itcast.flink.redis;

import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Author itcast
 * Date 2022/1/21 17:31
 * Desc TODO
 */
public class RedisDemo {

    static JedisPool jedisPool = null;

    static {
        jedisPool = RedisUtils.redisConnectionPool();
    }

    /**
     * 测试是否连接成功，打印所有的keys
     */
    @Test
    public void testConnect() {
        Jedis jedis = jedisPool.getResource();
        Set<String> keySet = jedis.keys("*");
        for (String s : keySet) {
            System.out.print(s + " ");
        }
    }

    @Test
    public void stringOpTest() {
        Jedis connection = jedisPool.getResource();
        // 1.  添加一个string类型数据，key为pv，初始值为0
        connection.set("pv", "0");

        // 2.  查询该key对应的数据
        System.out.println("原始pv为:" + connection.get("pv"));

        // 3.  修改pv为1000
        connection.set("pv", "1000");
        System.out.println("修改pv为:" + connection.get("pv"));

        // 4.  实现整形数据原子自增操作 +1
        connection.incr("pv");
        System.out.println("pv自增1：" + connection.get("pv"));

        // 5.  实现整形该数据原子自增操作 +1000
        connection.incrBy("pv", 1000);
        System.out.println("pv自增1000：" + connection.get("pv"));

    }

    @Test
    public void hashOpTest() {
        Jedis connection = jedisPool.getResource();
        // 1.  往Hash结构中添加以下商品库存
        // a)  iphone11 => 10000
        // b)  macbookpro => 9000
        connection.hset("goodsStore", "iphone11", "10000");
        connection.hset("goodsStore", "macbookpro", "9000");

        // 2.  获取Hash中所有的商品
        Map<String, String> keyValues = connection.hgetAll("goodsStore");
        for (String s : keyValues.keySet()) {
            System.out.println(s + " => " + keyValues.get(s));
        }

        // 3.  修改Hash中macbookpro数量为12000
        // 方式一：
        // connection.hset("goodsStore", "macbookpro", "12000");
        // 方式二：
        connection.hincrBy("goodsStore", "macbookpro", 3000);
        System.out.println("新增3000个库存后：macbookpro => " + connection.hget("goodsStore", "macbookpro"));

        // 4.  删除整个Hash的数据
        connection.del("goodsStore");
    }

    @Test
    public void listOpTest() {
        Jedis connection = jedisPool.getResource();
        // 1.  向list的左边插入以下三个手机号码：18511310001、18912301231、18123123312
        connection.lpush("telephone", "18511310001", "18912301231", "18123123312");

        // 2.  从右边移除一个手机号码
        connection.rpop("telephone");
        //从左边删除一个手机号码
        connection.lpop("telephone");

        // 3.  获取list所有的值
        List<String> telList = connection.lrange("telephone", 0, -1);
        for (String tel : telList) {
            System.out.print(tel + " ");
        }
    }

    @Test
    public void setOpTest() {
        Jedis connection = jedisPool.getResource();

        // 1.  往一个set中添加页面 page1 的uv，用户user1访问一次该页面
        connection.sadd("page1", "user1");

        // 2.  user2访问一次该页面
        connection.sadd("page1", "user2");

        // 3.  user1再次访问一次该页面
        connection.sadd("page1", "user1");

        // 4.  最后获取 page1的uv值
        Long uv = connection.scard("page1");
        System.out.println("page1页面的UV为：" + uv);
    }
}
